import org.apache.commons.io.FileUtils;

import java.io.*;
import java.util.Arrays;
import java.util.Collection;

/**
 * @Author: zhuchunfeng
 * @Description
 * @Date: 2020/1/30 10:53
 * Copyright (c) 2019 北京新媒传信科技有限公司
 */
public class Bill_ALI {
    //GBK编码格式源码路径
    public static  String srcDirPath = "C:\\Users\\zhu.chunfeng\\Desktop\\project\\gitee\\demo-parent\\5easy-excel\\src\\main\\resources\\zifubao";
    //转为UTF-8编码格式源码路径
    public static String utf8DirPath = "C:\\Users\\zhu.chunfeng\\Desktop\\project\\gitee\\demo-parent\\5easy-excel\\src\\main\\resources\\zifubao_";


    public static void main_(String[] args) throws IOException {

//获取所有java文件
        Collection<File> javaGbkFileCol =  FileUtils.listFiles(new File(srcDirPath), null, true);

        for (File javaGbkFile : javaGbkFileCol) {
            //UTF8格式文件路径
            String utf8FilePath = utf8DirPath+javaGbkFile.getAbsolutePath().substring(srcDirPath.length());
            //使用GBK读取数据，然后用UTF-8写入数据
            FileUtils.writeLines(new File(utf8FilePath), "UTF-8", FileUtils.readLines(javaGbkFile, "GBK"));
        }
    }

    public static Double 支出 = 0d;
    public static Double 收入 = 0d;

    public static final String WEI_XIN = utf8DirPath;

    public static void main(String[] args) {
        File file = new File(WEI_XIN);
        File[] files = file.listFiles();
        Arrays.stream(files).forEach(
                f ->{
                    File[] files1 = f.listFiles();
                    if (files1.length != 2) {
                        throw new RuntimeException("exception1");
                    }
                    File wei = Arrays.stream(files1).filter(file1 -> file1.getName().endsWith("账务明细_1.csv")).findFirst().get();
                    try {
                        test(wei);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
        );

        System.out.println("收入" + 收入);
        System.out.println("支出" + 支出);
    }

    private static void handlefile(File wei) throws Exception  {
        System.out.println(wei.getName());
        FileReader reader = new FileReader(wei);

    }

    public static void test(File wei) {

//        System.out.println(wei.getName());
        try {
            File fileWriter = new File("C:\\Users\\zhu.chunfeng\\Desktop\\project\\gitee\\demo-parent\\5easy-excel\\src\\main\\java\\支付宝账单(20170701-20200130).csv");
            FileWriter fileWriter1 = new FileWriter(fileWriter, true);

            //先FileReader把文件读出来再bufferReader按行读  reader.readLine(); 没有标题用不着了
            BufferedReader reader = new BufferedReader(new FileReader(wei));
            String line = null;
            while ((line = reader.readLine()) != null) {
                String[] item = line.split(",");//一行数组
                if (item.length > 6 && item[5].contains("杰培")) {
                    System.out.println(Arrays.asList(item));
                    fileWriter1.write(line+"\n");
                }

            }
            fileWriter1.flush();
            fileWriter1.close();
        } catch (Exception e) {
            //在命令行打印异常信息在程序中出错的位置及原因。
            e.printStackTrace();
        }
    }

}
